package com.example.shiro.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @author Jiange
 * @create 2021-05-07-16:23
 */
@Configuration
public class ShiroConfig {
    @Bean
    UserRealm userRealm(){
        return new UserRealm();
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);
        /**
         * anon:无需认证
         * authc：必须认证了才能访问
         * user：必须拥有记住我功能才能用
         * perms：拥有对某个资源的权限才能访问
         * role：表示拥有某个角色权限
         */
        LinkedHashMap<String, String> stringStringHashMap = new LinkedHashMap<>();

//        stringStringHashMap.put("/vip1","authc");
        stringStringHashMap.put("/vip1","perms[user----add]");
        stringStringHashMap.put("/","anon");
        stringStringHashMap.put("/index","anon");

        bean.setFilterChainDefinitionMap(stringStringHashMap);

        bean.setLoginUrl("/login");
        return bean;
    }
}
